<template>
    <el-dialog width="400px" title="修改密码" :visible.sync="dialogFormVisible" @close='onClose'>
        <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="用户名" prop="name">
                {{ userInfo.username }}
            </el-form-item>
            <el-form-item label="原密码" prop="password">
                <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="新密码" prop="newPassword">
                <el-input type="password" v-model="ruleForm.newPassword" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="确认密码" prop="checkPass">
                <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
                <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
</template>

<script>
import { clearLoginInfo } from '@/utils'
import { UpdatePasswordAPI } from '@/http';
export default {
    name: 'update-page',
    /**
    * 定义组件的data函数，用于声明组件内部的状态。
    * 
    * @returns {Object} 返回一个对象，其中包含组件内部的状态。这些状态将在组件的生命周期内被管理和使用。
    */
    data() {
        var validatePass = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入密码'));
            } else {
                if (this.ruleForm.checkPass !== '') {
                    this.$refs.ruleForm.validateField('checkPass');
                }
                callback();
            }
        };
        var validatePass2 = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请再次输入密码'));
            } else if (value !== this.ruleForm.newPassword) {
                callback(new Error('两次输入密码不一致!'));
            } else {
                callback();
            }
        };
        return {
            dialogFormVisible: false,
            userInfo: {},
            ruleForm: {},
            rules: {
                name: [{}],
                password: [{ required: true, message: '请输入原密码', trigger: 'blur' },],
                newPassword: [
                    { required: true, message: '请输入新密码', trigger: 'blur' },
                    { validator: validatePass, trigger: 'blur' }
                ],
                checkPass: [
                    { required: true, message: '请输入确认密码', trigger: 'blur' },
                    { validator: validatePass2, trigger: 'blur' }
                ],
            }
        }
    },
    created() {
        this.userInfo = (JSON.parse(sessionStorage.getItem('userInfo') || '{}')).user
        console.log('user', this.userInfo);
    },
    methods: {
        onClose() {
            this.$emit('close')
        },
        open() {
            this.dialogFormVisible = true;
        },
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {

                    let params = {
                        'password': this.$md5(this.ruleForm.password),
                        'newPassword': this.$md5(this.ruleForm.newPassword)
                    }
                    UpdatePasswordAPI(params).then(res => {
                        if (res.code === 200) {
                            this.dialogFormVisible = false;
                            this.$message({
                                message: '修改密码成功！',
                                type: 'success'
                            });
                            this.$router.push('/login');
                            clearLoginInfo()
                        }
                    })
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
    }
}
</script>

<style scoped></style>